function ui_success(msg,callback) {
	$('#toast .weui_toast_content').html(msg);
	$('#toast').show();
	setTimeout(function () {
		$('#toast').hide();
		if (callback != undefined) {
			callback();
		}
	}, 1000);
}

function ui_error(msg,callback) {
	$('#toast2 .weui_toast_content').html(msg);
	$('#toast2').show();
	setTimeout(function () {
		$('#toast2').hide();
		if (callback != undefined) {
			callback();
		}
	}, 2000);
}

function ui_alert(msg) {
	$('#dialog2 .weui_dialog_bd').html(msg);
	$('#dialog2').show();
	$('#dialog2').find('.weui_btn_dialog').on('click', function () {
		$('#dialog2').hide();
	});
}

function ui_confirm(msg, callback, cancel,title) {
	if(title == undefined){
		title = '提示';
	}
	$('#dialog1 .weui_dialog_hd').html(title);
	$('#dialog1 .weui_dialog_bd').html(msg);
	$('#dialog1').show();
	$('#dialog1').find('.weui_dialog_ft .default').on('click', function () {
		if (cancel != undefined) {
			cancel();
		}
		$('#dialog1').hide();
	});
	$('#dialog1').find('.weui_dialog_ft .primary').on('click', function () {
		if (callback != undefined) {
			callback();
		}
		$('#dialog1').hide();
	});
}

/* ajax提交*/
function sendGetAjax(url,callback,type) {
	if(typeof(type) == "undefined"){
		type = "json";
	}
	return $.ajax({
		type : "GET",
		url : url,
		dataType : type,
		success : callback,
		error: function(XMLHttpRequest, textStatus, errorThrown) {
			var dataObj = eval("("+XMLHttpRequest.responseText+")");
			if(dataObj.errmsg != ''){
				ui_error(dataObj.errmsg);
			}
		}
	});
}

/*提交表单*/
function sendForm(formId, post_url, return_url) {
	if (check_form(formId)) {
		$('.post_form_save').attr('disabled', "true");

		if ($("#ajax").val() == 1) {
			var vars = $("#" + formId).serialize();
			$.ajax({
				type: "POST",
				url: post_url,
				data: vars,
				dataType: "json",
				success: function (data) {
					$('#loadingToast').hide();
					if (data.code == 0) {
						ui_success(data.message, function () {
							if (return_url) {
								window.location.href = return_url;
							} else if (data.url) {
								window.location.href = data.url;
							}else{
								window.location.reload();
							}
						});
					} else {
						$('.post_form_save').removeAttr("disabled");
						ui_error(data.message);
					}
				},
				error: function (XMLHttpRequest, textStatus, errorThrown) {
					$('#loadingToast').hide();
					var dataObj = eval("(" + XMLHttpRequest.responseText + ")");
					if (dataObj.message != '') {
						$('.post_form_save').removeAttr("disabled");
						ui_error(dataObj.message);
					}
				}
			});
		} else {
			$("#" + formId).attr("action", post_url);
			$('.post_form_save').removeAttr("disabled");
			$("#" + formId).submit();
		}
	}
}


function check_form(form_id) {
	last_submit = $('#' + form_id).data('last_submit');
	current_submit = new Date().getTime();
	if (last_submit == undefined) {
		$('#' + form_id).data('last_submit', new Date().getTime());
	} else {
		if (current_submit - last_submit > 600) {
			$('#' + form_id).data('last_submit', new Date().getTime());
		} else {
			return false;
		}
	}

	var check_flag = true;
	$("#" + form_id + " :input").each(function(i) {
		if ($(this).attr("check")) {
			if (!validate($(this).val(), $(this).attr("check"))) {
				ui_error($(this).attr("msg"));
				$(this).focus();
				check_flag = false;
				return check_flag;
			}
		}
	});
	return check_flag;
}


/* 验证数据类型*/
function validate(data, datatype) {
	if (datatype.indexOf("|")) {
		tmp = datatype.split("|");
		datatype = tmp[0];
		data2 = tmp[1];
	}
	switch (datatype) {
		case "require":
			if (data == "") {
				return false;
			} else {
				return true;
			}
			break;
		case "email":
			var reg = /^([0-9A-Za-z\-_\.]+)@([0-9a-z]+\.[a-z]{2,3}(\.[a-z]{2})?)$/g;
			return reg.test(data);
			break;
		case "password":
			var reg = /^[\@A-Za-z0-9\!\#\$\%\^\&\*\.\~]{6,22}$/;
			return reg.test(data);
			break;
		case "number":
			var reg = /^[0-9]+\.{0,1}[0-9]{0,3}$/;
			return reg.test(data);
			break;
		case "mobile":
			var reg = /(^1[3|4|5|7|8][0-9]{9}$)/;
			return reg.test(data);
			break;
		case "html":
			var reg = /<...>/;
			return reg.test(data);
			break;
		case "eqt":
			data2 = $("#" + data2).val();
			return data >= data2;
			break;
	}
}

function closeTopAd(obj,base_url){
	sendGetAjax(base_url+'/Index/closeAd',function(data){
		if(data.code == 0){
			obj.parent().hide();
		}
	})
}
/**
 * @author hongchen
 * @param post_url 要提交的URL
 * @param object_id 收藏的ID
 * @param link_url 收藏的连接
 * @param pic 收藏的图片
 * @param title 收藏的标题
 * @param type 收藏的类型
 */
function userCollect(post_url, object_id, link_url, pic, title, type)
{
	$.post(post_url, {object_id:object_id, link_url:link_url, pic:pic, title:title, type:type},function(result){
		if(result.code == 0){
			ui_success(result.message);
		}else{
			ui_error(result.message);
		}
	}, 'json');
}